package com.litong.litongjiaoyu.controller;

import com.alibaba.fastjson.JSONObject;
import com.litong.litongjiaoyu.annotation.IgnoreAuth;
import com.litong.litongjiaoyu.entity.SysReservation;
import com.litong.litongjiaoyu.service.ReservationService;
import com.litong.litongjiaoyu.utils.JSONResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ReservationController
 * @Description 课程预约控制层
 * @Author jingz
 * @Date 2019/12/3 10:02
 * @Version 1.0
 **/
@Log4j2
@RestController
@RequestMapping("/reservation")
public class ReservationController {

    @Autowired
    private ReservationService reservationService;

    /**
     * 查询全部课程预约
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @GetMapping("/query")
    public JSONResult query() {
        log.info("开始获取全部课程预约数据");
        List<SysReservation> reservations = reservationService.query();

        if (reservations.size() > 0) {
            log.info("获取到数据，总长度：" + reservations.size());
            return JSONResult.ok(reservations);
        } else {
            log.error("未获取到数据，请检查是否出现errorMsg");
            return JSONResult.errorException("null");
        }
    }

    /**
     * 查询是试听还是预约
     * @param jsonObject 微信小程序传来的json数据
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @PostMapping("/classification")
    public JSONResult queryByTypes(@RequestBody JSONObject jsonObject) {
        List<SysReservation> reservations = reservationService.classification(jsonObject.getString("type"),
                jsonObject.getString("campus"));

        if (reservations.size() > 0) {
            log.info("获取到数据，总长度：" + reservations.size());
            return JSONResult.ok(reservations);
        } else {
            log.error("未获取到数据，请检查是否出现errorMsg");
            return JSONResult.errorException("未获取到数据，请检查是否出现errorMsg");
        }
    }
    
    /**
     * 获取一对一历史记录
     * @param jsonObject 手机号
     * @return json数据
     */
    @ResponseBody
    @PostMapping("/getfacetofacerecords")
    public JSONResult getFaceToFaceRecords(@RequestBody JSONObject jsonObject) {
        List<SysReservation> sysReservations = reservationService.getFaceToFaceAppointmentRecords(jsonObject.getString("phone"));
    
        if (sysReservations.size()>0) {
            return JSONResult.ok(sysReservations);
        } else {
            return JSONResult.ok(null);
        }
    }

    /**
     * 添加并发送给指定老师
     * @param sysReservation
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @PostMapping("/add")
    public JSONResult add(@RequestBody SysReservation sysReservation){
       try {
           log.info("开始添加并发送课程预约信息");

           sysReservation.setStatus(0);

           if(reservationService.add(sysReservation) > 0) {
               return JSONResult.ok();
           } else {
               return JSONResult.errorMsg("fail");
           }
       } catch (Exception e){
           log.error(e.getMessage());
           return JSONResult.errorException(e.getMessage());
       }
    }


    @ResponseBody
    @PostMapping("/update")
    public JSONResult update(@RequestBody JSONObject jsonObject){
        log.error(jsonObject.toJSONString());
        SysReservation sysReservation = jsonObject.getObject("reservation", SysReservation.class);
        
        log.info("开始更新内容");
        try {
            if (reservationService.update(sysReservation) > 0){
                log.info("修改预约数据成功");
                return JSONResult.ok();
            } else {
                log.error("修改预约数据失败");
                return JSONResult.errorMsg("fail");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return JSONResult.errorException(e.getMessage());
        }
    }


    @ResponseBody
    @GetMapping("/deleteAll")
    public JSONResult deleteAll() {
        return JSONResult.ok(reservationService.deleteAll());
    }
}
